package com.madi.company;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.daynightmodeutils.ChangeModeController;
import com.madi.company.ui.mainfragment.ApplyFragment;
import com.madi.company.ui.mainfragment.ChatFragment;
import com.madi.company.ui.mainfragment.HomeFragment;
import com.madi.company.ui.mainfragment.PersonFragment;
import com.madi.company.ui.mainfragment.ResumeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 *  baseactivity
 *
 */
public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    @Bind(R.id.layFrame)
    FrameLayout mFlBody;
    @Bind(R.id.bottom_navigation_bar)
    BottomNavigationBar bottomNavigationBar;

    private List<Fragment> list = new ArrayList<>();
    private HomeFragment mHomeFragment;
    private ApplyFragment mApplyFragment;
    private ChatFragment mChatFragment;
    private ResumeFragment mResumeFragment;
    private PersonFragment mPersonFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        //初始化菜单
        initTab();
    }
    /**
     * 初始化tab
     */
    private void initTab() {
        /**
         *添加标签的消息数量
         */
        BadgeItem numberBadgeItem = new BadgeItem()
                .setBorderWidth(4)
                .setBackgroundColor(Color.RED)
                .setText("5")
                .setHideOnSelect(true);
        /**
         *添加tab标签页
         */
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.home_icon_on, getString(R.string.home)).setActiveColorResource(R.color.F1).setInactiveIconResource(R.mipmap.home_icon))//.setBadgeItem(numberBadgeItem)
                .addItem(new BottomNavigationItem(R.mipmap.home_myrelease_on, getString(R.string.mypublish)).setActiveColorResource(R.color.F1).setInactiveIconResource(R.mipmap.home_myrelease))
                .addItem(new BottomNavigationItem(R.mipmap.home_chat_on, getString(R.string.chat)).setBadgeItem(numberBadgeItem).setActiveColorResource(R.color.F1).setInactiveIconResource(R.mipmap.home_chat))
                .addItem(new BottomNavigationItem(R.mipmap.home_edit_on, getString(R.string.mypublish)).setActiveColorResource(R.color.F1).setInactiveIconResource(R.mipmap.home_edit))
                .addItem(new BottomNavigationItem(R.mipmap.home_user_on, getString(R.string.home_person_tab)).setActiveColorResource(R.color.F1).setInactiveIconResource(R.mipmap.home_user))//.setBadgeItem(numberBadgeItem)
                .initialise();
        /**
         *首次进入不会主动回调选中页面的监听
         *所以这里自己调用一遍，初始化第一个页面
         */
        onTabSelected(0);
        bottomNavigationBar.setTabSelectedListener(this);
    }
    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        /**
         *每次添加之前隐藏所有正在显示的Fragment
         *然后如果是第一次添加的话使用transaction.add();
         *但第二次显示的时候,使用transaction.show();
         *这样子我们就可以保存Fragment的状态了
         */
        hideFragment(transaction);
        switch (position) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                    transaction.add(R.id.layFrame, mHomeFragment);
                    list.add(mHomeFragment);
                } else {
                    transaction.show(mHomeFragment);
                }
                break;
            case 1:
                if (mApplyFragment == null) {
                    mApplyFragment = new ApplyFragment();
                    transaction.add(R.id.layFrame, mApplyFragment);
                    list.add(mApplyFragment);
                } else {
                    transaction.show(mApplyFragment);
                }
                break;
            case 2:
                if (mChatFragment == null) {
                    mChatFragment = new ChatFragment();
                    transaction.add(R.id.layFrame, mChatFragment);
                    list.add(mChatFragment);
                } else {
                    transaction.show(mChatFragment);
                }
                break;
            case 3:
                if (mResumeFragment == null) {
                    mResumeFragment = new ResumeFragment();
                    transaction.add(R.id.layFrame, mResumeFragment);
                    list.add(mResumeFragment);
                } else {
                    transaction.show(mResumeFragment);
                }
                break;
            case 4:
                if (mPersonFragment == null) {
                    mPersonFragment = new PersonFragment();
                    transaction.add(R.id.layFrame, mPersonFragment);
                    list.add(mPersonFragment);
                } else {
                    transaction.show(mPersonFragment);
                }
                break;
        }
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }
    /**
     * @param transaction
     */
    public void hideFragment(FragmentTransaction transaction) {
        for (Fragment fragment : list) {
            transaction.hide(fragment);
        }
    }

    @Override
    public void onTabReselected(int position) {

    }
    /**
     * 监听全屏视频时返回键
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    /**
     * 监听返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ChangeModeController.onDestory();
    }
}
